/**
 * 无尽模式入口排行
 */
class EndLessSamllRankUI extends UIBase{
    private btn_close:BaseButton;
    private btn_state:BaseButton;
    private lb_num:eui.Label;
    private btn_rank:eui.Label;
    private btn_start:BaseButton;
    private lb_vedio_cnt:eui.Label;
    private lb_share_cnt:eui.Label;
    private btn_video:BaseButton;
    private btn_share:BaseButton;
    private rank_scroller:eui.Scroller;
    private rank_list:eui.List;
    private _listDataArr:eui.ArrayCollection;

    private endLessInfo = {
        ticket_num: 0,
        can_video_num: 0,
        can_share_num: 0
    }
    constructor()
    {
        super('EndLessSmallRankView',{showBG:true});
    }

    public reviewHide()
    {
        // if(RoleCache.instance().reviewStatus)
        // {
        //     this.lb_share_cnt.visible = false;
        //     this.lb_vedio_cnt.visible = false;
        //     this.btn_share.visible = false;
        //     this.btn_video.visible = false;
        // }
        // else
        // {
        //     this.lb_share_cnt.visible = true;
        //     this.lb_vedio_cnt.visible = true;
        //     this.btn_share.visible = true;
        //     this.btn_video.visible = true;
        //     this.btn_state.visible = true;
        // }
    }

    public init()
    {
        super.init(true);
        this.reviewHide();
        this.requestInfo();
    }

    public addEvent()
    {
        this.btn_close.addTapHandler(this.close, this);
        this.btn_state.addTapHandler(this.openStateUI, this);
        this.btn_share.addTapHandler(this.shareHandle, this);
        this.btn_start.addTapHandler(this.startGame, this);
        this.btn_video.addTapHandler(this.vedioHandle, this);
        this.btn_rank.addEventListener(egret.TouchEvent.TOUCH_TAP, this.openAllRank, this);
        GameUtil.addEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    protected initUI()
    {
        this.updateUI();
        this.initRank();
    }

    public initRank()
    {
        let rankList = this.endLessInfo['rank'];
        this._listDataArr = new eui.ArrayCollection(rankList);
        this.rank_list.itemRenderer = EndLessRankItemUI;
        this.rank_list.dataProvider = this._listDataArr;
    }

    protected shareSuccess(e:BaseEvent)
    {
        let type = e.param['type'];
        let self = this;
        if(type == ShareId.ticket_share)
        {
            self.addTickets('share');
        }
    }

    public requestInfo()
    {
        let param = {
            version:GameConst.version
        }
        HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorendless/get_info', StringUtil.jsonToHttpPostString(param), (e)=>{
            let data = JSON.parse(e);
            if(data && data.data)
            {
                this.endLessInfo = data.data;
                this.initUI();
            }
        }, this);
    }

    protected vedioHandle()
    {
        let cnt = this.endLessInfo.can_video_num;
        if(cnt <= 0){
            MessageUtil.tips('已经没有机会了哦！');
            return;
        }
        // this.addTickets('video');
        let self = this;
        if(egret.Capabilities.os == "Windows PC")
        {
           this.addTickets('video');
        }
        else
        {
            let self = this;
            let Success_call = function()
            {  
                self.addTickets('video');
            };
            let Error_call = function()
            {  
                MessageUtil.tips('观看完整视频才能获得入场券哦！');
            };
            WeChatgGameManager.instance().showVideoAd(GameConst.adList.endless_vedio, Success_call, Error_call, this);
        }
    }

    protected startGame()
    {
        let self = this;
        let ticket_num = this.endLessInfo.ticket_num;
        if(ticket_num > 0)
        {
            HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorendless/enter_endless', null, (e)=>{
                let data = JSON.parse(e);
                if(data.msg == 'ok')
                {
                    ScreenMovies.MovieStart(2,()=>{
                        let da = {
                            index:0
                        }
                        UIManager.instance().openUILogic(EndLessUI, da);
                    },self);
                }
                else
                {
                    MessageUtil.tips('扣除入场券失败！ ');
                }
            }, this);
        }
        else
        {
            MessageUtil.tips('您的入场券不足哦~');
        }
    }

    protected openAllRank()
    {
        let data = {
            isPop:true,
            effect:2
        }
        UIManager.instance().openUILogic(WorldRankUI, data);
    }

    protected addTickets(type)
    {
         let self = this;
         let data = {
             type:type,
             version:GameConst.version
         }
         UIManager.instance().showNormalLoading();
         HttpManager.instance().sendPost(GameConst.httpUrl + 'warriorendless/add_ticket', StringUtil.jsonToHttpPostString(data), (e)=>{
                UIManager.instance().hideNormalLoading();
                let data = JSON.parse(e);
                if(data.msg == 'ok')
                {
                    if(type == 'video')
                    {
                        self.endLessInfo.can_video_num --;
                    }
                    else
                    {
                        self.endLessInfo.can_share_num --;
                    }
                    self.endLessInfo.ticket_num++;
                    self.updateUI();
                    MessageUtil.tips('增加成功！');
                }   
                else
                {
                    MessageUtil.tips('增加失败！');
                }
            }, this);
    }

    protected updateUI()
    {
        this.lb_num.text = this.endLessInfo.ticket_num + '';
        this.lb_vedio_cnt.text = `可观看${this.endLessInfo.can_video_num}次`;
        this.lb_share_cnt.text = `可分享${this.endLessInfo.can_share_num}次`;
    }

    protected openStateUI()
    {
        let data = {
            isPop:true,
            effect:1,
            showBG:true
        }
        UIManager.instance().openUILogic(EndLessStateUI, data);
    }

    protected shareHandle()
    {
        let cnt = this.endLessInfo.can_share_num;
        if(cnt <= 0){
            MessageUtil.tips('已经没有机会了哦！');
            return;
        }
        // this.addTickets('share');
        SharePrase.instance().requestOneAdvertisement({type:ShareId.ticket_share});
    }

    protected close()
    {
        this.remove(1);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        this.btn_state.removeTapHandler();
        this.btn_start.removeTapHandler();
        this.btn_video.removeTapHandler();
        this.btn_rank.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.openAllRank, this);
        GameUtil.removeEventListener(EventConst.SHARE_SUCCESS, this.shareSuccess, this);
    }

    public clear()
    {
        super.clear();
        this.endLessInfo = null;
    }
}